package com.epam.edu.command;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class SignOutCommand implements Command {
    @Override
    public String perform(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        Cookie[] cookies = request.getCookies();
        int number = -1;
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                if ("userId".equals(cookies[i].getName())) {
                    number = i;
                }
            }
        }
        if (number != -1) {
            cookies[number].setMaxAge(0);
            response.addCookie(cookies[number]);
        }
        return "/index.jsp";
    }
}
